const fs = require("fs");
const {BOOK_PATH}= require("../constants/file-path")
const { getBookImageById, addbookInfo, getAllBook, getBookById,
    getBookByName,AddBorrow, ModifyStatus, FindBorrowList,deleteBookById,FindBorrowAllList,getBorrowByName } = require("../service/book_service")
class BookController{
    //发布书籍
    async AddbookInfo(ctx, next) {
        let bookInfo=ctx.request.body
        let {id} = await addbookInfo(bookInfo);
        ctx.body = {
            id,
            status: 200,
            message:"发布成功"
        }
    }
    //获取书籍列表
    async getBooksInfo(ctx, next) {
        let bookList = await getAllBook()
        ctx.body = {
            bookList,
            status: 200,
            message:"获取图书列表成功"
        }
    }
    //书籍搜索
    async SearchBookByName(ctx, next) {
        let { keyValue } = ctx.request.body;
        let bookList = await getBookByName(keyValue)
        ctx.body = {
            bookList,
            status: 200,
            message:"搜索成功"
        }
    }
    //获取书籍详情
    async getBookDetail(ctx, next) {
        let { id } = ctx.params;
        let res = await getBookById(id)
        ctx.body=res
    }
    //书籍借阅
    async borrowBook(ctx, next) {
        let { username: borrower } = ctx.user;
        let { bookid, owner } = ctx.request.body;
        let res = await AddBorrow(bookid, owner, borrower);
        //改变书籍enable为false状态
        await ModifyStatus(bookid);
        let status = res ? 200 : 400;
        let message = res ? "借阅成功" : "借阅失败";
        ctx.body = {
            status,
            message
        }
    }
    //获取借阅读书列表
    async borrowList(ctx,next) {
        try {
            let { username } = ctx.user
            let res = await FindBorrowList(username)
            res = res.filter(item => {
                return item.borrower == username||item.book.owner==username
            })
            ctx.body=res
        } catch (error) {
            console.log(error)
        }
       
    }
    //获取全部借阅列表
    async borrowAllList(ctx,next) {
        try {
            let borrowList = await FindBorrowAllList()
            ctx.body = {
                borrowList,
                status: 200,
                message:"获取借阅列表成功"
            }
        } catch (error) {
            console.log(error)
        }
    }
    //修改书籍封面
    async bookImageInfo(ctx, next) {
        const { bookId } = ctx.params;
        const bookImageInfo = await getBookImageById(bookId);
        ctx.response.set("content-type", "png")
        
        ctx.body = fs.createReadStream(`${BOOK_PATH}/${bookImageInfo.filename}`);
    }
    //删除书籍
    async deleteBook(ctx, next) {
        const { id } = ctx.params;
        await deleteBookById(id);
        ctx.body = {
            status: 200,
            message:"删除成功"
        }
    }
    //搜索借阅书籍
    async SearchBorrowByName(ctx, next) {
        let {keyValue} = ctx.request.body;
        let borrowList = await getBorrowByName(keyValue)
        ctx.body = {
            borrowList,
            status: 200,
            message:"搜索成功"
        }
    }
}
module.exports=new BookController()